//
//  SWBookViewController.swift
//  YiyaPuzzleDemo
//
//  Created by linhan on 16/9/29.
//  Copyright © 2016年 test. All rights reserved.
//

import Foundation
import UIKit
class SWBookViewController: SWBaseViewController,SWImageCropperViewControllerDelegate
{
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillLayoutFirstTime() {
        setup()
    }
    
    
    private func setup()
    {
        //presentCropperViewController()
        bookSetup()
        
        let bookBtn:UIButton = UIButton(type: .system)
        bookBtn.setTitle("我的相册", for: .normal)
        bookBtn.sizeToFit()
        bookBtn.center = view.center
        bookBtn.addTarget(self, action: #selector(bookBtnTapped), for: .touchUpInside)
        view.addSubview(bookBtn)
    }
    
    private func bookSetup()
    {
        SWBookAssetManager.sharedManager().launch()
        SWDownloadManager.sharedManager().launch()
        SWPageManager.sharedManager().launch()
        
        if SWBookManager.sharedManager().setuped
        {
            presentBook()
        }
        else
        {
            SWBookManager.sharedManager().setupedCallback = {[weak self] in
                self?.presentBook()
            }
            SWBookManager.sharedManager().setup()
        }
    }
    
    private func presentBook()
    {
        let bookID:String = "456"
        let books = SWBookManager.sharedManager().books
        var book:SWBook?
        let template = loadBookTemplate()
        if let index = books.index(where: {($0.bookID ?? "") == bookID}),
            let aBook = books.valueAt(index)
        {
            print("已缓存记录")
            book = aBook
        }
        else
        {
            print("无缓存记录，创建新书")
            let aBook = createBookFromTemplate(template, bookID: bookID)
            book = aBook
            SWBookManager.sharedManager().addBook(aBook)
        }
        
        if let book = book
        {
            SWBookManager.sharedManager().editingBook(book)
            if template.version > (book.version)
            {
                book.updateFromTemplate(template)
            }
            
            SWBookManager.sharedManager().save()
            let viewController = SWPageEditorViewController(book:book)
            //let nav = SWBaseNavigationController(rootViewController: viewController)
            //self?.presentViewController(nav, animated: true, completion: nil)
            navigationController?.pushViewController(viewController, animated: true)
        }
    }
    
    private func loadBookTemplate() -> SWBookTemplate
    {
        let template:SWBookTemplate = SWBookTemplate()
        template.version = 1
        
        let page1:SWPageTemplate = createPageTemplate(2)
        page1.pageID = "1"
        
        let page2:SWPageTemplate = createPageTemplate(3)
        page2.pageID = "2"
        
        let page3:SWPageTemplate = createPageTemplate(4)
        page3.pageID = "3"
        
        let page4:SWPageTemplate = createPageTemplate(5)
        page4.pageID = "4"
        page4.version = 6
        
        let page5:SWPageTemplate = createPageTemplate(6)
        page5.pageID = "5"
        page5.version = 1
        
        var pages:[SWPageTemplate] = []
        pages.append(page1)
        pages.append(page2)
        pages.append(page3)
        pages.append(page4)
        //pages.append(page5)
        template.pages = pages
        
        return template
    }
    
    private func createPageTemplate(_ type:Int) -> SWPageTemplate
    {
        if type == 0
        {
            let page1:SWPageTemplate = SWPageTemplate()
            page1.largeBgURL = "http://7xj0zo.com1.z0.glb.clouddn.com/page_1.png"
            page1.previewBgURL = "http://7xj0zo.com1.z0.glb.clouddn.com/page_1_preview.png"
            //page1.editable = true
            
            var page1Photos:[SWPhotoTemplate] = []
            let page1Photo1:SWPhotoTemplate = SWPhotoTemplate()
            page1Photo1.xPercent = 0.185
            page1Photo1.yPercent = 0.505
            page1Photo1.widthPercent = 0.308
            page1Photo1.heightPercent = 0.225
            
            let page1Photo2:SWPhotoTemplate = SWPhotoTemplate()
            page1Photo2.xPercent = 0.495
            page1Photo2.yPercent = 0.505
            page1Photo2.widthPercent = 0.308
            page1Photo2.heightPercent = 0.225
            
            let page1Photo3:SWPhotoTemplate = SWPhotoTemplate()
            page1Photo3.xPercent = 0.185
            page1Photo3.yPercent = 0.734
            page1Photo3.widthPercent = 0.308
            page1Photo3.heightPercent = 0.225
            
            let page1Photo4:SWPhotoTemplate = SWPhotoTemplate()
            page1Photo4.xPercent = 0.495
            page1Photo4.yPercent = 0.734
            page1Photo4.widthPercent = 0.308
            page1Photo4.heightPercent = 0.225
            
            page1Photos.append(page1Photo1)
            page1Photos.append(page1Photo2)
            page1Photos.append(page1Photo3)
            page1Photos.append(page1Photo4)
            page1.photos = page1Photos
            
            return page1
        }
        else if type == 1
        {
            let page:SWPageTemplate = SWPageTemplate()
            page.largeBgURL = "http://7xj0zo.com1.z0.glb.clouddn.com/page_2.png"
            page.previewBgURL = "http://7xj0zo.com1.z0.glb.clouddn.com/page_2_preview.png"
            //page2.editable = true
            
            let photo:SWPhotoTemplate = SWPhotoTemplate()
            photo.xPercent = 0.4
            photo.yPercent = 0.0386
            photo.widthPercent = 0.558
            photo.heightPercent = 0.428
            
            let text:SWTextTemplate = SWTextTemplate()
            text.defaultText = "合成后的小图；当此页面不是本人编辑的，则只加载预览小图"
            text.textColor = "0xFF0000"
            text.xPercent = 0.05
            text.yPercent = 0.587
            text.widthPercent = 0.912
            text.heightPercent = 0.23
            
            page.photos = [photo]
            page.texts = [text]
            
            return page
        }
        else if type == 2
        {
            let page:SWPageTemplate = SWPageTemplate()
            page.largeBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_1.png"
            page.previewBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_1_preview.png"
            
            let photo:SWPhotoTemplate = SWPhotoTemplate()
            photo.xPercent = 0.1811
            photo.yPercent = 0.1715
            photo.widthPercent = 0.6378
            photo.heightPercent = 0.4678
            
            page.photos = [photo]
            
            return page
        }
        else if type == 3
        {
            let page:SWPageTemplate = SWPageTemplate()
            page.largeBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_2.png"
            page.previewBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_2_preview.png"
            
            let photo1:SWPhotoTemplate = SWPhotoTemplate()
            photo1.xPercent = 0.1372
            photo1.yPercent = 0.1514
            photo1.widthPercent = 0.4116
            photo1.heightPercent = 0.3019
            
            let photo2:SWPhotoTemplate = SWPhotoTemplate()
            photo2.xPercent = 0.1372
            photo2.yPercent = 0.4670
            photo2.widthPercent = 0.4116
            photo2.heightPercent = 0.3019
            
            let text:SWTextTemplate = SWTextTemplate()
            text.defaultText = "宝贝，时间既绵长又短促，妈妈已经习惯感受你每天在妈妈肚子里的成长，感受你每一次伸手、踢腿…想到你就要离开温暖的堡垒，来到我和爸爸的身边，心里又期待又不舍。[ 甜蜜的负担 ]大概就是这个意思吧。"
            text.textColor = "0xd3d3d3"
            text.xPercent = 0.5884
            text.yPercent = 0.3623
            text.widthPercent = 0.3293
            text.heightPercent = 0.0870
            
            page.photos = [photo1, photo2]
            page.texts = [text]
            
            return page
        }
        else if type == 4
        {
            let page:SWPageTemplate = SWPageTemplate()
            page.largeBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_3.png"
            page.previewBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_3_preview.png"
            
            let photo1:SWPhotoTemplate = SWPhotoTemplate()
            photo1.xPercent = 0.3732
            photo1.yPercent = 0.4267
            photo1.widthPercent = 0.2547
            photo1.heightPercent = 0.1868
            
            let photo2:SWPhotoTemplate = SWPhotoTemplate()
            photo2.xPercent = 0.6476
            photo2.yPercent = 0.4267
            photo2.widthPercent = 0.2547
            photo2.heightPercent = 0.1868
            
            page.photos = [photo1, photo2]
            
            return page
        }
        else if type == 5
        {
            let page:SWPageTemplate = SWPageTemplate()
            page.largeBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_4.png"
            page.previewBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_4_preview.png"
            
            let photo1:SWPhotoTemplate = SWPhotoTemplate()
            photo1.xPercent = 0.1021
            photo1.yPercent = 0.2077
            photo1.widthPercent = 0.2525
            photo1.heightPercent = 0.1852
            
            let photo2:SWPhotoTemplate = SWPhotoTemplate()
            photo2.xPercent = 0.6476
            photo2.yPercent = 0.2077
            photo2.widthPercent = 0.2525
            photo2.heightPercent = 0.1852
            
            let photo3:SWPhotoTemplate = SWPhotoTemplate()
            photo3.xPercent = 0.1021
            photo3.yPercent = 0.4058
            photo3.widthPercent = 0.2525
            photo3.heightPercent = 0.1852
            
            let photo4:SWPhotoTemplate = SWPhotoTemplate()
            photo4.xPercent = 0.3754
            photo4.yPercent = 0.4058
            photo4.widthPercent = 0.2525
            photo4.heightPercent = 0.1852
            
            let text:SWTextTemplate = SWTextTemplate()
            text.defaultText = "宝宝，你知道么？以后你会离开爸爸妈妈，走好多好多的路，去到不同的地方，住进各种各样的屋子…你看，这是你来到世界第一个房间。"
            text.textColor = "0xcbcbcb"
            text.xPercent = 0.3622
            text.yPercent = 0.6387
            text.widthPercent = 0.5049
            text.heightPercent = 0.0370
            
            page.photos = [photo1, photo2, photo3,photo4]
            page.texts = [text]
            
            return page
        }
        else if type == 6
        {
            let page:SWPageTemplate = SWPageTemplate()
            page.largeBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_5.png"
            page.previewBgURL = "http://ofjl8s3rm.bkt.clouddn.com/test/page_5_preview.png"
            
            let photo:SWPhotoTemplate = SWPhotoTemplate()
            photo.xPercent = 0.1350
            photo.yPercent = 0.1578
            photo.widthPercent = 0.7300
            photo.heightPercent = 0.5354
            
            page.photos = [photo]
            
            return page
        }
        
        return SWPageTemplate()
    }
    
    //从模板中创建一本新书
    private func createBookFromTemplate(_ template:SWBookTemplate, bookID:String) -> SWBook
    {
        let book:SWBook = SWBook()
        book.bookID = bookID
        if let pageTemplates = template.pages?.flatMap({$0 as? SWPageTemplate})
        {
            var pages:[SWPage] = []
            for template in pageTemplates
            {
                let page = SWPage(template: template)
                page.bookID = bookID
                pages.append(page)
            }
            book.pages = pages
        }
        return book
    }
    
    private func presentCropperViewController()
    {
        if let image = UIImage(named: "room_bg.jpg")
        {
            let vc = SWImageCropperViewController(image: image)
            vc.delegate = self
            present(vc, animated: true, completion: nil)
        }
    }
    
    func imageCropperViewController(_ viewController:SWImageCropperViewController, didFinishCroppingImage croppedImage:UIImage)
    {
        print("croppedImage:", croppedImage.size, croppedImage.scale)
        let imageView = UIImageView(image: croppedImage)
        imageView.frame = CGRectMake(10, 10, 300, 300)
        imageView.contentMode = .scaleToFill
        view.addSubview(imageView)
    }
    
    
    @objc private func bookBtnTapped()
    {
        presentBook()
    }
    
    
}
